/*******************************************************************************
 * @project: fant
 * @package: com.acconsys.ant.tasks
 * @file: CopyFileTask.java
 * @author: bruce
 * @created: 2010-11-10
 * @purpose:
 * 
 * @version: 1.0
 * 
 * Revision History at the end of file.
 * 
 * Copyright 2010 AcconSys All rights reserved.
 ******************************************************************************/

package com.acconsys.ant.tasks;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.Task;

public class CopyFileTask extends Task {
	
	private String outputEncode;
	private String inputFile;
	private String outputFile;

	@Override
	public void execute() throws BuildException {
		try {
			OutputStreamWriter out = null;
			if (outputEncode == null || outputEncode.trim().length() == 0) {
				out = new OutputStreamWriter(new FileOutputStream(outputFile), "gb2312");
			} else {
				out = new OutputStreamWriter(new FileOutputStream(outputFile), outputEncode.trim());
			}

			SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			String lineSeperator = System.getProperty("line.separator");
			out.write("-- Copy Time: ");
			out.write(dateFormat.format(new Date()));
			
			BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(inputFile), "UTF-8"));

            String line = null;
            while ((line = br.readLine()) != null) {
				out.write(lineSeperator);
				out.write(line);
            }
            br.close();
			out.flush();
			out.close();

			log("[fant:CopyFileTask] " + inputFile);
		} catch (Exception e) {
			throw new BuildException(e.getMessage());
		}
	}

	public String getOutputEncode() {
		return outputEncode;
	}

	public void setOutputEncode(String outputEncode) {
		this.outputEncode = outputEncode;
	}

	public String getInputFile() {
		return inputFile;
	}

	public void setInputFile(String inputFile) {
		this.inputFile = inputFile;
	}

	public String getOutputFile() {
		return outputFile;
	}

	public void setOutputFile(String outputFile) {
		this.outputFile = outputFile;
	}

}



/*******************************************************************************
 * <B>Revision History</B><BR>
 * [type 'revision' and press Alt + / to insert revision block]<BR>
 * 
 * 
 * 
 * Copyright 2010 AcconSys All rights reserved.
 ******************************************************************************/